import { ProFormOptions, FormItemProps } from '@/components/pro-form/type'
import { GridItemProps } from 'naive-ui'
import { InStoreModel } from '@/api/model/asset-management/asset-in-store-model'
import { Ref } from 'vue'
import useGetWarehouseList from '@/hooks/useGetWarehouseList'

type Props = {
  fetchGetPurchaseDetail: (data: any) => void
  typeListLength: Ref<number>
  purchaseLoading: Ref<boolean>
  purchaseOptions: Ref<any>
}

export const useInStoreProForm = ({
  fetchGetPurchaseDetail,
  typeListLength,
  purchaseLoading,
  purchaseOptions
}: Props) => {
  const [inStoreForm, resetInStoreForm] = useProRef<Partial<InStoreModel>>({})
  const [inStoreItemForm, resetInStoreItemForm] = useProRef<
    Partial<InStoreModel>
  >({})
  const { typeOptions, typeLoading } = useGetTypeList()
  const { warehouseOptions, warehouseLoading } = useGetWarehouseList()

  const inStoreFormOptions = computed<ProFormOptions>(() => {
    const list = [
      {
        formType: 'input',
        key: 'inStoreName',
        label: '入库单名称',
        placeholder: '请输入入库单名称'
      },
      {
        formType: 'select',
        key: 'purchaseId',
        label: '采购名称',
        placeholder: '请选择采购名称',
        options: purchaseOptions.value,
        loading: purchaseLoading.value,
        onUpdateValue: fetchGetPurchaseDetail
      },
      {
        formType: 'textarea',
        key: 'remark',
        label: '备注',
        gridItem: {
          span: 2
        },
        placeholder: '请输入备注',
        noRule: true
      }
    ] as (FormItemProps & {
      gridItem?: GridItemProps
      noRule?: boolean
      ruleType?: string
    })[]
    return useSetOptions(list)
  })

  const inStoreItemFormOptions = computed<ProFormOptions>(() => {
    const list: Array<any> = []
    for (let i = 0; i < typeListLength.value; i++) {
      list.push(
        {
          formType: 'select',
          key: 'typeId' + i,
          label: '资产类型' + (i + 1),
          options: typeOptions.value,
          loading: typeLoading.value,
          disabled: true
        },
        {
          formType: 'input',
          key: 'assetName' + i,
          label: '资产名称',
          placeholder: '请输入资产名称'
        },
        {
          formType: 'input',
          key: 'brand' + i,
          label: '品牌',
          placeholder: '请输入品牌',
          noRule: true
        },
        {
          formType: 'inputNumber',
          min: 0,
          precision: 2,
          key: 'price' + i,
          label: '单价',
          placeholder: '请输入单价'
        },
        {
          formType: 'input',
          key: 'unit' + i,
          label: '单位',
          placeholder: '请输入单位',
          noRule: true
        },
        {
          formType: 'input',
          key: 'model' + i,
          label: '规格型号',
          placeholder: '请输入规格型号',
          noRule: true
        },
        {
          formType: 'input',
          key: 'function' + i,
          label: '功能描述',
          placeholder: '请输入功能描述',
          noRule: true
        },
        {
          formType: 'select',
          key: 'warehouseId' + i,
          label: '存储位置',
          placeholder: '请选择存储位置',
          options: warehouseOptions.value,
          loading: warehouseLoading.value
        },
        {
          formType: 'inputNumber',
          min: 1,
          precision: 0,
          key: 'number' + i,
          label: '入库数量',
          placeholder: '请输入入库数量'
        },
        {
          formType: 'textarea',
          key: 'remark' + i,
          label: '备注',
          rows: 1,
          gridItem: {
            span: 1
          },
          placeholder: '请输入备注',
          noRule: true
        }
      )
    }
    return useSetOptions(list)
  })

  return {
    inStoreForm,
    inStoreFormOptions,
    inStoreItemForm,
    inStoreItemFormOptions,
    resetInStoreForm,
    resetInStoreItemForm
  }
}
